<?php
class ReportService{
    //输入类型
    const NONE = 0;
    const TEXT = 1;
    const SELECT = 2;
    const RANGE = 3;
    public function getValueByEles($uid,$items,$type = array(EvaluateReportLog::TYPE_YJX,EvaluateReportLog::TYPE_USER))
    {
        if(!is_array($items)){
            return false;
        }
        //转换
        $name = array();
        $map = array();
        foreach ($items as $key => $it) {
            array_push($name, $it["name"]);
            $map[$it["name"]] = $it["field_name"];
        }
        
        $ret = EvaluateReportLog::model()->findByAttributes(array(
                "uid"=>$uid,"type"=>$type,),
                array("order"=>"id desc"));
        if($ret === null){
            return array();
        }
        $ret = EvaluateReportCache::model()->findAllByAttributes(
                array("no"=>$ret->no,"name"=>$name));
        $rets = array();
        foreach ($ret as $it) {
            $item["name"]=$it->name;
            $item["value"]=$it->value;
            $item["issue"]=$it->issue=="↑"?1:0;
            $item["normal"]=$it->normal;
            $item["unit"]=$it->unit;
            
            $rets[$map[$it->name]] = $item;
        }
        return $rets;
    }
    /**
     * 
     * @param array $items 体检项目
     * @return boolean
     */
    public function saveQuestionItems($uid,$items)
    {
        $trans = Yii::app()->db->beginTransaction();
        try {
            $ret = EvaluateReportLog::model()->findByAttributes(array("uid"=>$uid,"type"=>  EvaluateReportLog::TYPE_FXPG),array("order"=>"id desc"));
            if($ret === null){
                $ret = new EvaluateReportLog();
                $ret->type = EvaluateReportLog::TYPE_FXPG;
                $ret->uid = $uid;
                if($ret->save()===false){
                    return false;
                }
                $iid = sprintf("%06d",$ret->id);
                $ret->no = date("YmdHis").$iid;
                if($ret->update()===false){
                    throw new Exception("新建风险评估体检项目保存记录");
                }
            }
            foreach ($items as $key => $it) {
                $item = $this->getReportInfo($key);
                if($item!=null){
                    $item["value"] = $it;
                }
                $t = EvaluateReportCache::model()->findByAttributes(array(
                    "uid"=>$uid,
                    "no"=>$ret->no,
                    "name"=>$item["name"],
                    "parent_name"=>$item["parent_name"]
                ));
                if($t === null){
                    $t = new EvaluateReportCache();
                    $t->uid = $uid;
                    $t->no = $ret->no;
                    $t->name = $item["name"];
                    $t->value = $item["value"];
                    $t->parent_name = $item["parent_name"];
                    $t->issue = $this->getIssue($item["value"],$item["normal"]);
                    $t->normal = $item["normal"];
                    $t->unit = $item["unit"];
                    if(false === $t->save()){
                        throw new Exception("保存体检项失败");
                    }
                }else{
                    $t->value = $item["value"];
                    $t->issue = $this->getIssue($item["value"],$item["normal"]);
                    if(false === $t->update()){
                        throw new Exception("保存体检项失败");
                    }
                }
            }
            $trans->commit();
        } catch (Exception $exc) {
            $trans->rollback();
            return false;
        }
        return true;
    }
    /**
     * 保存体检项目
     */
    public function saveUserReport($uid,$no,$items)
    {
        $sql="INSERT INTO evaluate_report_cache (`uid`,`no`,`parent_name`,"
                . "`name`,`value`,`issue`,`normal`,`unit`) VALUES";
            foreach($items as $it){
                $value = $it["value"];
                $normal = isset($it["normal"])?$it["normal"]:"";
                
                if(strpos($normal, "~")!==false){
                    
                    $v = explode("~", $normal);
                    if((float)$value<(float)$v[0]){
                        $it["issue"] = "↓";
                    }elseif((float)$value>(float)$v[1]){
                        $it["issue"] = "↑";
                    }
                }
                $issue = isset($it["issue"])?$it["issue"]:"";
                $unit = isset($it["unit"])?$it["unit"]:"";
                $sql .= "('$uid','$no','{$it["parent_name"]}','{$it["name"]}','$value',"
                        . "'$issue','$normal','$unit'),";
            }
            
            
            if(count($items)){
                $num=Yii::app()->db->createCommand(substr($sql,0,-1))->execute();
                if($num){
                    return true;
                }
            }
            return false;
    }

    /**
     * 
     * @param type $disids
     * @return boolean
     */
    private function getIssue($value,$normal)
    {
        $v = explode("~", $normal);

        if(count($v)!=2){
            return "";
        }
        if($value>$v[1]){
            return "↑";
        }elseif($value<$v[0]){
            return "↓";
        }else{
            return "";
        }
    }
    public function getDiseaseReportTemplate($disids = array())
    {
        if(!is_array($disids)){
            return false;
        }
        if(count($disids)==0){
            $ret = EvaluateDiseaseType::model()->findAll();
            foreach ($ret as $it) {
                array_push($disids, $it->id);
            }
        }
        $items = array();
        foreach ($disids as $did){
            switch ($did){
                case Disease::GAOXUEYA:
                    $items['blood_triglycerides'] = $this->getReportInfo('blood_triglycerides');
                    break;
                case Disease::GUANXINBING:
                    $items['blood_triglycerides'] = $this->getReportInfo('blood_triglycerides');
                    break;
                case Disease::NAOZUZHONG:
                    $items['blood_triglycerides'] = $this->getReportInfo('blood_triglycerides');
                    break;
                case Disease::TANGNIAOBING;
                    $items['blood_triglycerides'] = $this->getReportInfo('blood_triglycerides');
                    $items['blood_sugar'] = $this->getReportInfo('blood_sugar');
                    break;
                default :
                    break;
            }
        }
  
        return $items;
    }
    private function getReportInfo($itemname) 
    {
        $items['blood_triglycerides'] = array(
            "name" => "甘油三酯",
            "parent_name"=>"血生化",
            "value" => "", 
            "issue" => 0, 
            "normal" => "0.51~1.70",
            "unit" => "mmol/L");
        $items['blood_sugar'] = array(
            "name" => "空腹血糖",
            "parent_name"=>"血生化",
            "value" => "",
            "issue" => 0,
            "normal" => "3.61~6.11",
            "unit" => "mmol/L");
        return isset($items[$itemname]) ? $items[$itemname] : array();
    }
    /**
     * 获取体检报告录入信息
     */
    public function getReportList()
    {
        $items[] = array("name"=>"身高", "parent_name"=>"一般性检查","type"=>  ReportService::SELECT,"option"=>"70~240","unit"=>"cm","default"=>175);
        $items[] = array("name"=>"体重", "parent_name"=>"一般性检查","type"=>  ReportService::SELECT,"option"=>"20~150","unit"=>"kg","default"=>70);
        $items[] = array("name"=>"收缩压", "parent_name"=>"一般性检查","type"=>  ReportService::SELECT,"option"=>"70~200","default"=>110,"unit"=>"mmHg","normal"=>"90~139");
        $items[] = array("name"=>"舒张压", "parent_name"=>"一般性检查","type"=>  ReportService::SELECT,"option"=>"30~180","default"=>70,"unit"=>"mmHg","normal"=>"60~89");

         /*血生化*/
        $items[] = array("name"=>"空腹血糖", "parent_name"=>"血生化","type"=>  ReportService::RANGE,"unit"=>"mmol/L","normal"=>"3.61~6.11","step"=>0.01,"option"=>"2.0~15.0","default"=>5.5);
        $items[] = array("name"=>"总胆固醇", "parent_name"=>"血生化","type"=>  ReportService::RANGE,"unit"=>"mmol/L","normal"=>"2.86~5.98","step"=>0.01,"option"=>"2.0~15.0","default"=>4.5);
        $items[] = array("name"=>"甘油三酯", "parent_name"=>"血生化","type"=>  ReportService::RANGE,"unit"=>"mmol/L","normal"=>"0.51~1.70","step"=>0.01,"option"=>"0.0~3.0","default"=>1.0);
        $items[] = array("name"=>"丙氨酸氨基转移酶（ALT）", "parent_name"=>"血生化","type"=>  ReportService::RANGE,"unit"=>"U/L","normal"=>"0~40","step"=>1,"option"=>"0~50","default"=>20);
        $items[] = array("name"=>"天冬氨酸氨基转移酶", "parent_name"=>"血生化","type"=>  ReportService::RANGE,"unit"=>"U/L","normal"=>"0~40","step"=>1,"option"=>"0~50","default"=>20);
        $items[] = array("name"=>"尿酸", "parent_name"=>"血生化","type"=>  ReportService::RANGE,"unit"=>"umol/L","normal"=>"90~355","step"=>5,"option"=>"50~450","default"=>200);
        
        /*尿常规*/
        $items[] = array("name"=>"尿白细胞", "parent_name"=>"尿常规","type"=>  ReportService::RANGE,"unit"=>"hp","normal"=>"0~5","step"=>0.1,"option"=>"-1.5~6.5");
		$tiems[] = array("name"=>"尿红细胞","parent_name"=>'尿常规',"type"=>ReportService::TEXT);
        $items[] = array("name"=>"尿比重", "parent_name"=>"尿常规","type"=>  ReportService::RANGE,"unit"=>"","normal"=>"1.003~1.030","step"=>0.001,"option"=>"0.995~1.038");
        $items[] = array("name"=>"尿酸碱度", "parent_name"=>"尿常规","type"=>  ReportService::RANGE,"unit"=>"","normal"=>"4.0~8.5","step"=>0.01,"option"=>"2.7~9.8");
        
        $items[] = array("name"=>"心率", "parent_name"=>"内科","type"=>  ReportService::SELECT,"option"=>"40~150","unit"=>"次/分钟","default"=>50);
        $items[] = array("name"=>"心律", "parent_name"=>"内科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"心音", "parent_name"=>"内科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"肺部听诊", "parent_name"=>"内科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"肝脏触诊", "parent_name"=>"内科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"脾脏触诊", "parent_name"=>"内科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"肾脏叩诊", "parent_name"=>"内科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"内科其它", "parent_name"=>"内科","type"=>  ReportService::TEXT);
        
        $items[] = array("name"=>"皮肤", "parent_name"=>"外科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"浅表淋巴结", "parent_name"=>"外科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"甲状腺(外科)", "parent_name"=>"外科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"乳房", "parent_name"=>"外科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"脊柱", "parent_name"=>"外科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"四肢关节", "parent_name"=>"外科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"肛门、直肠指诊", "parent_name"=>"外科","type"=>  ReportService::TEXT);            
        
		$items[] = array("name"=>"裸视力(右)", "parent_name"=>"眼科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"裸视力(左)", "parent_name"=>"眼科","type"=>  ReportService::TEXT);
        
        $items[] = array("name"=>"外耳", "parent_name"=>"耳鼻咽喉科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"外耳道", "parent_name"=>"耳鼻咽喉科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"鼓膜", "parent_name"=>"耳鼻咽喉科","type"=>  ReportService::TEXT);
        
        $items[] = array("name"=>"口腔粘膜", "parent_name"=>"口腔科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"牙齿", "parent_name"=>"口腔科","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"唇", "parent_name"=>"口腔科","type"=>  ReportService::TEXT);
        
        /**妇科***/
        
        /*血常规*/
        $items[] = array("name"=>"红细胞计数", "parent_name"=>"血常规","type"=>  ReportService::TEXT,"unit"=>"mmHg");
        $items[] = array("name"=>"尿糖", "parent_name"=>"血常规","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"尿白细胞", "parent_name"=>"血常规","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"尿亚硝酸盐", "parent_name"=>"血常规","type"=>  ReportService::TEXT);
        $items[] = array("name"=>"蛋白质", "parent_name"=>"血常规","type"=>  ReportService::TEXT);
         
         
       
       
       
        
        
        return $items;
    }
    
    

}
?>